<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>文章维护
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" @click="ALL" class="lkx_r20">全部</el-button>
        <div class="inline_div">
          <el-input
            v-model.trim="select_word"
            placeholder="输入文章ID和文章名称"
            class="handle-input mr10"
            @keyup.enter.native="search_key(select_word)"
          ></el-input>
          <el-button type="primary" icon="search" @click="search_key(select_word)">搜索</el-button>
        </div>
        <div class="inline_div">
          <el-input
            v-model.trim="media_key"
            placeholder="输入媒体账号"
            class="handle-input mr10"
            @keyup.enter.native="adminkey(media_key)"
          ></el-input>
          <el-button type="primary" icon="search" @click="adminkey(media_key)">搜索</el-button>
        </div>
        <div class="inline_div">
          <span class="zh_status">文章分类选择:</span>
          <el-select
            v-model="artice_value"
            placeholder="请选择"
            class="status_select"
            @change="select_sort"
          >
            <el-option
              v-for="item in articleoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <div class="inline_div">
          <span class="zh_status">文章状态选择:</span>
          <el-select
            v-model="status_value"
            placeholder="请选择"
            class="status_select"
            @change="select_status"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <el-button type="danger" @click="rect">一键清空</el-button>
      </div>
      <el-table
        border
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="news_name" label="文章名称" align="center"></el-table-column>
        <el-table-column prop="news_id" label="文章ID" align="center"></el-table-column>
        <el-table-column label="发布时间" align="center">
          <template slot-scope="scope">{{ scope.row.release_time }}</template>
        </el-table-column>       
        <el-table-column prop="status" label="文章状态" align="center"></el-table-column>
        <el-table-column prop="media_name" label="媒体账号" align="center"></el-table-column>
        <el-table-column prop="news_class" label="文章分类" align="center"></el-table-column>
        <el-table-column prop="review_admin_name" label="审核人员" align="center"></el-table-column>
        <el-table-column label="是否推荐至首页" width="180" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_recommend"
              :disabled="scope.row.TFs"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchs(scope.$index, scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >查看</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-count="pageTotal"
          :page-size="10"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "../../http/api.js";
export default {
  data() {
    return {
      //表格数据
      tableData: [],
      idx:-1,
      select_word: "", //搜索的文章ID和文章名称关键词的默认值
      media_key: "", //媒体账号里面关键词的默认值
      options: [
        {
          value: "0",
          label: "全部"
        },
        {
          value: "1",
          label: "已发布"
        },
        {
          value: "2",
          label: "已驳回"
        }
      ],
      articleoptions: [],
      status_value: "", //文章默认状态下拉
      artice_value: "", //文章分类下拉默认值
      delVisible: false, //点击删除弹出的信息框状态默认为不显示
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 2, //总页数
      news_id: "", //文章id 用来连接详情的参数
      typenumber: false //操作开启或者关闭
    };
  },
  methods: {
    getData(StatusPull, Class, newkey, mediaKey) {
      //获得表格数据！ 全部通过传参的方法！
      this.$post(api.news_list, {
        token: this.Token,
        page: this.currPage,
        status: StatusPull, // 可支持状态：1 审核通过 2 审核驳回未通过
        class_id: Class,
        news: newkey,
        media: mediaKey
      }).then(res => {
        if (res.status == "0") {
          this.tableData = res.data.map((val, index, arr) => {
            let jsons = [];
            jsons.news_name = val.news_name;
            jsons.news_id = val.news_id;
            jsons.release_time=val.release_time;
            jsons.status = val.status;
            jsons.apply_time = val.apply_time;
            jsons.news_class = val.news_class;
            jsons.is_review=val.is_review;
            jsons.review_admin_name=val.review_admin_name;//审核人员
            jsons.media_name = val.media_name; //媒体账号 用来搜索的
            jsons.btColor = val.btColor; // 操作开启或者关闭 对应的颜色
            jsons.is_recommend = val.is_recommend; // 是否是推荐的
            jsons.typenumber = val.typenumber; // 开启或者关闭 对应的数字
            jsons.TFs = val.TFs; //判断按钮是禁用
            return jsons;
          });
          this.tableData.forEach((val, index, arr) => {
            if (val.status == "1") {
              val.status = "正常";
                // 判断推荐首页状态
                if(val.is_recommend=="0"){///是否推荐 0 否 1是
                    val.is_recommend=false;
                }
                else{
                  val.is_recommend=true;
                }
            }
            if (val.status == "2") {
              // 这个比较特殊 如果里面是驳回的 点击按钮就不操作！
              val.status = "审核驳回未通过";
              arr[index].TFs = true; // switch按钮禁用
            }
            if(val.is_review=="0"){
              val.review_admin_name="免审文章"
            }
          });
          // 获取分页数据
          this.Totals = res.total;
          this.pageTotal = res.pageTotal;
          this.currPage = res.currPage;
        } else {
          console.log(res.msg);
        }
        if (res.status == "relogin") {
          this.$router.push("/login");
        }
      });
    },
    getSort() {
      this.$post(api.classification_list).then(res => {
        if (res.status == "0") {
          this.articleoptions = res.data.map((val, index, arr) => {
            let jsons = [];
            (jsons.value = val.class_id), (jsons.label = val.classify_name);
            return jsons;
          });
        }
        else {
          console.log(res.msg);
        }
        if (res.status == "relogin") {
          this.$router.push("/login");
        }
      });
    },
    search_key(select_word) {
      //搜索文章名称和id
      if (select_word == "") {
        this.$message.error(`搜索不能为空,请输入文章名称或者ID`);
        return;
      } else {
        this.currPage = 1;
        this.getData(
          this.status_value,
          this.artice_value,
          this.select_word,
          this.media_key
        );
      }
    },
    adminkey(adkey) {
      //搜索媒体账号名称
      if (adkey == "") {
        this.$message.error(`搜索不能为空,请输入媒体账号名称`);
        return;
      } else {
        this.currPage = 1;
        this.getData(
          this.status_value,
          this.artice_value,
          this.select_word,
          this.media_key
        );
      }
    },
    rect() {
      //重置顶部工具栏！
      this.select_word = "";
      this.media_key = "";
      this.artice_value = "";
      this.status_value = "";
      this.currPage = 1;
      this.getData(0);
    },
    //查看详情页面
    handleEdit(index, row) {
      //点击进去详情
      this.news_id = this.tableData[index].news_id; //广告id
      this.$router.push({ path: "/details", query: { id: this.news_id } });
    },
    select_sort() {
      //选择文章分类
      this.currPage = 1;
      this.getData(
        this.status_value,
        this.artice_value,
        this.select_word,
        this.media_key
      );
    },
    select_status() {
      //选择文章状态
      this.currPage = 1;
      this.getData(
        this.status_value,
        this.artice_value,
        this.select_word,
        this.media_key
      );
    },
    switchs(index, row) {
      //点击推荐上首页
      this.news_id = this.tableData[index].news_id; //广告id
      this.typenumber = this.tableData[index].is_recommend; //操作  是 否
      // 因为上面获取到的是 true 或者false 这里需要在此转换一下
      if(this.typenumber==true){
        this.typenumber=1;
      }
      else{
        this.typenumber=2;
      }
      this.$post(api.news_recommend, {
        token: this.Token,
        type: this.typenumber, // type 1：推荐 2：取消推荐
        news_id: this.news_id
      })
        .then(res => {
          if (res.status == "0") {
            this.$message({
              type: "success",
              message: "修改成功！"
            });
            setTimeout(() => {
              this.getData(
                this.status_value,
                this.artice_value,
                this.select_word,
                this.media_key
              );
            },1000);
          } else {
            this.$message.error(res.msg);
          }
          // 如果登录过期
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleDelete(index, row) {
      //删除按钮
      this.idx = index;
      this.delVisible = true;
      this.news_id=this.tableData[index].news_id;
    },
    deleteRow() {
      // 确定删除
      this.$post(api.news_check,{
          token:this.Token,
          news_id:this.news_id,
          type:5
      })
      .then(res=>{
        if(res.status=="0"){
          this.$message.success(`删除成功`);
          this.getData(
          this.status_value,
          this.artice_value,
          this.select_word,
          this.media_key
        );
        }
        else{
           this.$message.error(`删除失败`);
        }
        if(res.status=="relogin"){
          this.$router.push('/login')
        }
      })
      .catch(error=>{
        console.log(error)
      })
      this.delVisible = false;
      
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getData(
        this.status_value,
        this.artice_value,
        this.select_word,
        this.media_key
      );
    },
    ALL() {
      //显示全部！
      this.currPage = 1;
      this.status_value = "";
      this.artice_value = "";
      this.select_word = "";
      this.media_key = "";
      this.getData(
        this.status_value,
        this.artice_value,
        this.select_word,
        this.media_key
      );
    }
  },

  created() {
    this.getData();
    this.getSort(); //获取文章分类
  }
};
</script>
<style scoped>
.mr10 {
  margin-right: 10px;
}
.handle-input {
  width: 150px;
  display: inline-block;
}
.handle-box {
  padding-bottom: 20px;
}
.inline_div {
  display: inline-block;
  margin-right: 10px;
}
.zh_status {
  line-height: 32px;
  margin-left: 20px;
}
.status_select {
  margin: 0 10px;
}

.pagination_div {
  margin-top: 20px;
  text-align: right;
}
</style>